package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.PageResult;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //往套餐表保存数据
        setmealMapper.insert(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        if(!CollectionUtils.isEmpty(setmealDishes)){
            setmealDishes = setmealDishes.stream().map(
                    item -> {
                        item.setSetmealId(setmealDto.getId());
                        return item;
                    }
            ).collect(Collectors.toList());
            for (SetmealDish setmealDish : setmealDishes){
                setmealDish.setSetmealId(setmealDto.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }

    }

    @Override
    public PageResult<SetmealDto> findPage(int page, int pageSize, String name) {
        //1、构造Page分页对象
        Page myPage = new Page(page,pageSize);
        //2、构造查询条件
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        //3、执行查询
        setmealMapper.selectPage(myPage,queryWrapper);
        //4、得到套餐集合
        List<Setmeal> setmealList = myPage.getRecords();
        //5、将List<Setmeal>转为List<SetmealDto>
        List<SetmealDto> setmealDtoList = setmealList.stream().map(
                item -> {
                    SetmealDto setmealDto = new SetmealDto();
                    BeanUtils.copyProperties(item,setmealDto);
                    Category category = categoryMapper.selectById(item.getCategoryId());
                    if(category != null){
                        setmealDto.setCategoryName(category.getName());
                    }
                    return setmealDto;
                }
        ).collect(Collectors.toList());
        //6、将List<SetmealDto>设置Page对象中
        myPage.setRecords(setmealDtoList);
        return new PageResult<>(myPage.getTotal(),myPage.getRecords());
    }

    @Override
    public void deleteWithDish(List<Long> ids) {
        //查询套餐状态，确定是否可用删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        Integer count = setmealMapper.selectCount(queryWrapper);
        if(count > 0){
            //如果不能删除，则抛出一个业务异常
            throw new BusinessException("套餐正在售卖中，不能删除");
        }
        //如果可以删除，先删除套餐表中的数据---setmeal
        setmealMapper.deleteBatchIds(ids);
        //delete from setmeal_dish where setmeal_id in (1,2,3)
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        //删除关系表中的数据----setmeal_dish
        setmealDishMapper.delete(lambdaQueryWrapper);
    }

    @Override
    public R<String> uploadStatus(Integer status, List<Long> ids) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(ids != null,Setmeal::getId,ids);
        queryWrapper.orderByDesc(Setmeal::getPrice);

        List<Setmeal> list = setmealMapper.selectList(queryWrapper);
        for(Setmeal setmeal : list){
            if(setmeal != null){
                setmeal.setStatus(status);
                setmealMapper.updateById(setmeal);
            }
        }
        return R.success("售卖状态修改成功");
    }

    @Override
    public void updateWithFlavor(SetmealDto setmealDto) {
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Setmeal::getName,setmealDto.getName());
        lambdaQueryWrapper.ne(Setmeal::getId,setmealDto.getId());
        Integer count = setmealMapper.selectCount(lambdaQueryWrapper);
        if(count > 0){
            throw new BusinessException("菜品["+setmealDto.getName()+"]已经存在");
        }
        //更新菜品表
        setmealMapper.updateById(setmealDto);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishMapper.delete(queryWrapper);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //保存菜品口味表
        if(!CollectionUtils.isEmpty(setmealDishes)){
            for (SetmealDish setmealDish : setmealDishes){
                setmealDish.setSetmealId(setmealDto.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    @Override
    public SetmealDto findById(Long id) {
        //定义返回DishDto
        SetmealDto setmealDto = new SetmealDto();
        //根据菜品id查询菜品表
        Setmeal setmeal = setmealMapper.selectById(id);
        //将dish对象数据copy到DishDto中
        BeanUtils.copyProperties(setmeal,setmealDto);
        //根据菜品id查询菜品口味表
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> selectList = setmealDishMapper.selectList(lambdaQueryWrapper);
        setmealDto.setSetmealDishes(selectList);
        return setmealDto;
    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealMapper.selectList(queryWrapper);
        return list;
    }


}
